﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchoolApplication.Models;
using GameSchoolApplication.Repositories;
using GameSchoolApplication.Classes;
using GameSchoolApplication.Models.ViewModels;
using System.IO;

namespace GameSchoolApplication.Controllers
{
    public class LevelController : Controller
    {

        [HttpGet]
        public ActionResult LevelIndex(int levelID)
        {
            CourseLevelRepository rep = new CourseLevelRepository();
            
            return View(rep.getLevelByID(levelID));
        }

        #region Level actions


        // GET: /Level/
        /// <summary>
        /// Returns the edit level page for a specific level. 
        /// Precondition: The user clicks the edit page for the level in question
        /// Postcondition: The user has recived the edit view for that level. 
        /// Author: Krilli
        /// Date:  7.5
        /// </summary>
        public ActionResult editLevel(int levelID)
        {
            //Initialize the repository
            CourseLevelRepository rep = new CourseLevelRepository();
            //Fetch the level
            CourseLevel level = rep.getLevelByID(levelID); 
            //Return the level and the view
            return View(level);
        }

        /// <summary>
        /// Submits level edit information to the database
        /// Precondition: The user has entered edit information into the view form 
        /// Postcondition: The information hs been submitted to the DB
        /// Author: Krilli
        /// Date: 7.5
        /// </summary>
        [HttpPost]
        public ActionResult editLevel(CourseLevel level)
        {
            //Initialize the repository
            CourseLevelRepository rep = new CourseLevelRepository();
            bool worked = rep.editLevel(level);

            //TODO: These return statements need to be edited with the proper view names when the views are done. 
            if (worked)
            {
                return View("Index", "Home");
            }
            else
            {
                return View("fail");
            }
            
        }


        /// <summary>
        /// Returns the level page of a specific level
        /// Precondition: The user has clicked the level's link
        /// Postcondition: The user has received the level view
        /// Author: Krilli
        /// Date: 7.5
        /// </summary>
        public ActionResult getLevel(int levelID)
        {
            //Initialize the repository
            CourseLevelRepository rep = new CourseLevelRepository();
            //Get the level and return it
            CourseLevel level = rep.getLevelByID(levelID); 
            
            return View(level);
        }

        /// <summary>
        /// returns levels for a given course in the form of a JSON string
        /// Author:Bjarmi
        /// </summary>
        /// <param name="courseID">id of the selected course</param>
        /// <returns>JSON</returns>
        public ActionResult getCourseLevels(int courseID)
        {
            //initialize the repository
            //select relevant data
            CourseLevelRepository rep = new CourseLevelRepository();
            List<LevelIcon> fake = rep.getLevelIconsOfCourse(courseID, Request.Cookies["userInfo"].Values["userID"]);
            return Json(fake,JsonRequestBehavior.AllowGet);
        }

        
        #endregion 

        #region Assignment actions

        /// <summary>
        /// Gets the add assignment view for the level in question
        /// Precondition: The user clicks the add assignment page 
        /// Postcondition: The user has recieved the add assignment formview
        /// Author: Krilli
        /// Date: 7.5
        /// </summary>
        public ActionResult addAssignment(int levelID)
        {
            CourseLevelRepository rep = new CourseLevelRepository(); 
            CourseLevel level = rep.getLevelByID(levelID);
            int courseID = level.cID.Value; 
            string userID = getUserIDFromCookie(); 
            if (isTeacherInCourse(userID, courseID))
            {
                Assignment ass = new Assignment();
                ass.lvlID = level.lvlID; 
                return View("addAssignment", ass);
            }
            else
            {
                return View("notTeacher"); 
            }
            
        }

        /// <summary>
        /// Adds a level to a course. 
        /// Precondition: The user has entered assignment information into the form
        /// Postcondition: The assignment has been added to the db
        /// Author: Krilli
        /// Date: 7.5
        /// </summary>
        [HttpPost]
        public ActionResult addAssignment(Assignment ass)
        {
            //Initialize the repository 
            AssignmentRepository rep = new AssignmentRepository();
            bool success = rep.addAssignment(ass);

            //Note: These return statements will need to be modified when the views are done. 
            if (success)
            {
                return View("win");
            }
            else
            {
                return View("fail");
            }
        }
    #endregion 
        
        #region Exam actions

        /// <summary>
        /// Returns the add exam view to the user
        /// Precondition: The user has clicked the add exam buton
        /// Postcondition: The user has received the add exam view
        /// Author: Krilli
        /// Date: 7.5
        /// </summary>
        public ActionResult addExam(int levelID)
        {
            CourseLevelRepository rep = new CourseLevelRepository(); 
            CourseLevel level = rep.getLevelByID(levelID);
            int courseID = level.cID.Value; 
            string userID = getUserIDFromCookie();
            if (isTeacherInCourse(userID, courseID))
            {
                Exam exam = new Exam();
                exam.lvlID = levelID;
                return View(exam);
            }
            else
            {
                return View("notTeacher"); 
            }
        }

        /// <summary>
        /// Adds a exam to the database
        /// Precondition: the user has entered exam information into the form 
        /// Postcondition: the exam is added to the db
        /// Author: Krilli 
        /// Date: 7.5
        /// </summary>
        /// 
        [HttpPost]
        public ActionResult addExam(Exam exam)
        {
            ExamRepository rep = new ExamRepository();
            int newExamID = rep.addExam(exam);
            //Note: These return statements need to be modified when the views are ready. 
            if (newExamID>=0)
            {
                return RedirectToAction("editExam", "Exam", new {examID = newExamID});
            }
            else
            {
                return View();
            }
        }
    #endregion 

        #region Material actions

        /// <summary>
        /// Returns the add material view for a specific level
        /// Precondition: The user has clicked the add material button for a specific level
        /// Postcondition: The user has received the add material view for that level
        /// Author: Krilli
        /// Date: 7.5
        /// </summary>
        public ActionResult addMaterial(int levelID)
        {
            CourseLevelRepository rep = new CourseLevelRepository();
            CourseLevel level = rep.getLevelByID(levelID); 
            return View(level);
        }

        /// <summary>
        /// Adds new material to the database
        /// Precondition: The user has entered the material information into the appropriate form 
        /// Postcondition: The material has been added to the DB
        /// Author: Krilli
        /// Date: 7.5
        /// </summary>
        public ActionResult addMaterial(Material mat)
        {
            MaterialRepository rep = new MaterialRepository();
            bool success = rep.addMaterial(mat);

            //Note: These return statements need to be modified when the views are done. 
            if (success)
            {
                return View();
            }
            else
            {
                return View();
            }
        }
     
    #endregion

        #region Achievement actions

        //Note; At time of writing (14:22 7.5) the achievement repository is not done, and so these classes remain todo

        /// <summary>
        /// Returns the add achievement form for a particular level
        /// Precondition: The user has clicked the add achievement button for the level 
        /// Postcondition: The user has received the appropriate add achievement form
        /// Author: Krilli
        /// Date: 7.5
        /// </summary>
        public ActionResult addAchievement(int levelID)
        {
            return View();
        }

        /// <summary>
        /// TODO: Write a cool-ass description!
        /// As of right now this is just a dummy class 
        /// Precondition:
        /// Postcondition:
        /// Author:
        /// Date: 
        /// </summary>
        //public ActionResult addAchievement(/*achievementModel*/)
        /*{
            return View();
        }*/
        /// <summary>
        /// TODO: Write a cool-ass description!
        /// As of right now this is just a dummy class 
        /// Precondition:
        /// Postcondition:
        /// Author:
        /// Date: 
        /// </summary>
        public ActionResult removeAchievement(/*achievementID*/)
        {
            return View();
        }
    #endregion

        #region Lecture actions
        /// <summary>
        /// Returns the add lecture view for a specific course
        /// Precondition: The user has clicked the add lecture link
        /// Postcondition: The user has received the add lecture form view
        /// Author: Krilli
        /// Date: 7.5
        /// </summary>
        /// <param name="levelID"></param>
        /// <returns></returns>
        public ActionResult addLecture(int levelID)
        {
            CourseLevelRepository rep = new CourseLevelRepository();
            CourseLevel level = rep.getLevelByID(levelID);
            return View(level);
        }

        [HttpPost]
        public ActionResult Uploadtest(HttpPostedFileBase file)
        {


            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(@"C:\Users\kannski\Documents\HR Skoli\verk14\gameschool-14\GameSchool\GameSchoolApplication\App_Data", fileName);
                file.SaveAs(path);
            }
            return RedirectToAction("Index");
        }
        /// <summary>
        /// Adds a lecture to the database. 
        /// Precondition: The user has entered lecture information into the form and submitted it
        /// Postcondition: The lecture has been added to the database. 
        /// Author: Krilli
        /// Date: 7.5
        /// </summary>
        ///public ActionResult addLecture(/*lectureModel*/)
       /// {
       ///     return View();
       /// }

   
    #endregion

        /// <summary>
        /// Determines if a user is a teacher in a course. 
        /// Precondition: The user exists, the course exists .
        /// Postcondition: A bool value is returned. 
        /// Author: Krill
        /// Date: 11.5
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="courseID"></param>
        /// <returns></returns>
        public bool isTeacherInCourse(string userID, int courseID)
        {
            AccountRepository rep = new AccountRepository();
            bool retVal = rep.isTeacherInCourse(userID, courseID);
            return retVal;
        }

        /// <summary>
        /// Gets the user ID from his cookie. 
        /// Precondition: The user has a cookied
        /// Postcondition: The user ID is returned.
        /// Author: Krilli
        /// Date: 11.5
        /// </summary>
        /// <returns></returns>
        public string getUserIDFromCookie()
        {
            string userID = Request.Cookies["userInfo"].Values["userID"];

            return userID;
        }

    }
}
